let nameEle = document.querySelector('input[name="name"]')
let passwordEle = document.querySelector('input[name="password"]')
let confirmPasswordEle = document.querySelector('input[name="confirmPassword"]')
let butEle = document.querySelector("button")
let inputEle = document.querySelectorAll("input")

let username = document.querySelector(".username")
let userpassword = document.querySelector(".userpassword")
let confPassword = document.querySelector(".confPassword")


// 判断信息不为空且账号密码符合要求注册
butEle.addEventListener("click", function (e) {
    e = e || window.event
    if (nameEle.value == "" || passwordEle.value == "" || confirmPasswordEle.value == "")
        return alert("填写信息不完整，请确认！")
    if (passwordCheck() == true || confPasswordCheck() == true)
     return alert("注册成功，请登录", location.href = "../pages/login.html")       // 保存账号密码在本地
    let nv = nameEle.value
    let pv = passwordEle.value
    let dataList={nv,pv}
    localStorage.setItem('USER',JSON.stringify(dataList));

})

// 用户名失焦事件
nameEle.onblur = function (e) {
    nameContent()
}

function nameContent() {

    if (nameEle.value == "") {
        username.innerHTML = "账户名不能为空"
        return false
    } else {
        nameEle.innerHTML = ""
        return true
    }
}


// 密码框失焦事件
passwordEle.onblur = function () {
    if (passwordContent() == false) {
        return
    }
    if (passwordCheck() == false) {
        return
    }
}

function passwordContent() {
    if (passwordEle.value == "") {
        userpassword.innerHTML = "密码不能为空"
        return false
    } else {
        passwordEle.innerHTML = ""
        return true
    }
}


// 密码框确认事件
confirmPasswordEle.onblur = function () {
    if (confirmPasswordContent() == false) {
        return
    }
    if (confPasswordCheck() == false) {
        return
    }
}

function confirmPasswordContent() {
    if (confirmPasswordEle.value == "") {
        confPassword.innerHTML = "密码不能为空"
        return false
    } else if (confirmPasswordEle.value !== passwordEle.value) {
        confPassword.innerHTML = "两次输入密码不同"
        return false

    } else {
        confirmPasswordEle.innerHTML = ""
        return true

    }

}
// 判断密码强弱
function passwordCheck() {
    let reg = /^[A-Z][0-9a-zA-Z]{6,}/
    let password = passwordEle.value
    if (!reg.test(password)) {
        userpassword.innerHTML = "数字字母组合，以大写字母开头，至少8位"
        return false
    } else {
        passwordEle.innerHTML = ""
        return true
    }
}
// 确认密码强弱
function confPasswordCheck() {
    let reg = /^[A-Z][0-9a-zA-Z]{6,}/
    let confpassword = confirmPasswordEle.value
    if (!reg.test(confpassword)) {
        confPassword.innerHTML = "数字字母组合，以大写字母开头，至少8位"
        return false
    } else {
        confirmPasswordEle.innerHTML = ""
        return true
    }
}